package com.gscitysfy.cus.drain.modules.command.convert;

import com.gscitysfy.cus.drain.modules.command.dto.SchemeManageDto;
import com.gscitysfy.cus.drain.modules.command.entity.SchemeManage;
import com.gscitysfy.cus.drain.modules.common.entity.DrainInspectionMnFile;
import com.gscitysfy.cus.drain.modules.common.mapper.DrainInspectionMnFileMapper;
import com.gscitysfy.cus.drain.utils.DateUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @Author jiang_wen
 * @Date 2022/10/20 19:46
 * @Description TODO
 */
@AllArgsConstructor
@Component
public class SchemeManageConvert {

    private final DrainInspectionMnFileMapper fileMapper;

    public SchemeManage toEntity(SchemeManage schemeManage, SchemeManageDto schemeManageDto) {
        schemeManage.setSchemeCategory(schemeManageDto.getSchemeCategory());
        schemeManage.setSchemeName(schemeManageDto.getSchemeName());
        schemeManage.setSchemeCode(schemeManageDto.getSchemeCode());
        schemeManage.setPlanId(schemeManageDto.getPlanId());
        schemeManage.setSchemeSource(schemeManageDto.getSchemeSource());
        schemeManage.setEnableFlag(schemeManageDto.getEnableFlag());
        schemeManage.setAuditStatus(schemeManageDto.getAuditStatus());
        schemeManage.setAuditContent(schemeManageDto.getAuditContent());
        schemeManage.setFileId(schemeManageDto.getFileId());
        return schemeManage;

    }

    public SchemeManageDto toDto(SchemeManage schemeManage) {
        SchemeManageDto schemeManageDto = new SchemeManageDto();
        schemeManageDto.setCmKid(schemeManage.getCmKid());
        schemeManageDto.setSchemeCategory(schemeManage.getSchemeCategory());
        schemeManageDto.setSchemeName(schemeManage.getSchemeName());
        schemeManageDto.setPlanId(schemeManage.getPlanId());
        schemeManageDto.setSchemeSource(schemeManage.getSchemeSource());
        schemeManageDto.setSchemeCode(schemeManage.getSchemeCode());
        schemeManageDto.setEnableFlag(schemeManage.getEnableFlag());
        schemeManageDto.setAuditContent(schemeManage.getAuditContent());

        schemeManageDto.setAuditStatus(schemeManage.getAuditStatus());
        if (StringUtils.isNotBlank(schemeManage.getAuditStatus())) {
            if (StringUtils.equals("PASS", schemeManage.getAuditStatus())) {
                schemeManageDto.setAuditStatusName("审核通过");
            } else if (StringUtils.equals("FAIL", schemeManage.getAuditStatus())) {
                schemeManageDto.setAuditStatusName("审核未通过");
            } else {
                schemeManageDto.setAuditStatusName(null);
            }
        }
        schemeManageDto.setFileId(schemeManage.getFileId());
        if (Objects.nonNull(schemeManage.getFileId())) {
            DrainInspectionMnFile drainInspectionMnFile = fileMapper.selectById(schemeManage.getFileId());
            schemeManageDto.setFileName(drainInspectionMnFile.getCmFileName());
        }
        schemeManageDto.setCmAddTime(Objects.nonNull(schemeManage.getCmAddtime()) ? DateUtil.dateToDateString(schemeManage.getCmAddtime(), "yyyy-MM-dd HH:mm:ss") : null);
        return schemeManageDto;
    }
}
